package hczc.longcai.com.hccrowdfunding.home.second_invest.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.asynctask.AddressAsyncTask;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;
import hczc.longcai.com.hccrowdfunding.home.second_invest.adapters.AffirmReturnAdapter;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.AffirmReturn;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.details_items.ConsigneeInfo;

public class AffirmReturnActivity extends BaseActivity implements View.OnClickListener, AffirmReturnAdapter.CallBack, AddressAsyncTask.CallBack {

    private static final String TAG = "AffirmReturnActivity";
    private ListView listView;
    private List<AffirmReturn> affirmReturns;
    private AffirmReturnAdapter adapter;
    private AffirmReturn affirm_address;
    private ConsigneeInfo info;
    private AddressAsyncTask addressAsyncTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_affirm_return);

        initDatas();
        initViews();
    }

    private void initDatas() {
        affirmReturns = new ArrayList<>();
        AffirmReturn affirmReturn1 = new AffirmReturn();
        affirmReturn1.setType(1);
        affirmReturns.add(affirmReturn1);
        AffirmReturn affirmReturn2 = new AffirmReturn();
        affirmReturn2.setType(2);
        affirmReturn2.setPrice("999元");
        affirmReturn2.setTab("应付金额");
        affirmReturns.add(affirmReturn2);
        AffirmReturn affirmReturn3 = new AffirmReturn();
        affirmReturn3.setType(2);
        affirmReturn3.setTab("支持金额");
        affirmReturn3.setPracticalPrice("999元");
        affirmReturns.add(affirmReturn3);
        AffirmReturn affirmReturn4 = new AffirmReturn();
        affirmReturn4.setType(2);
        affirmReturn4.setTab("配送费用");
        affirmReturn4.setFreight("免运费");
        affirmReturns.add(affirmReturn4);
        AffirmReturn affirmReturn5 = new AffirmReturn();
        affirmReturn5.setType(3);
        affirmReturn5.setReturnContent("感谢您的支持！众筹结束后您获得....感谢您的支持！众筹结束后您获得....感谢您的支持！众筹结束后您获得....");
        affirmReturns.add(affirmReturn5);
        AffirmReturn affirmReturn6 = new AffirmReturn();
        affirmReturn6.setType(4);
        affirmReturns.add(affirmReturn6);
        affirm_address = new AffirmReturn();
        affirm_address.setType(5);
//        info = new ConsigneeInfo();
//        affirm_address.setInfo(info);
        initAddress();
        affirmReturns.add(affirm_address);
        AffirmReturn affirmReturn8 = new AffirmReturn();
        affirmReturn8.setType(5);
        affirmReturn8.setRiskTip("文字内容描述");
        affirmReturns.add(affirmReturn8);
        AffirmReturn affirmReturn9 = new AffirmReturn();
        affirmReturn9.setType(6);
        affirmReturns.add(affirmReturn9);

    }

    private void initAddress() {
        addressAsyncTask = new AddressAsyncTask(this);
        addressAsyncTask.execute(((Void) null));
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("H基金");

        listView = ((ListView) findViewById(R.id.invest_affirm_listview));
        View view = LayoutInflater.from(this).inflate(R.layout.item_affirm_head, listView, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        listView.addHeaderView(view);
        adapter = new AffirmReturnAdapter(this, affirmReturns, this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            Bundle extras = data.getExtras();
            Address address = (Address) extras.getSerializable("address");
//            Log.d(TAG,"****************************"+address.toString());
            try {
                String name = address.getName();
                String phone = address.getPhone();
                String area = address.getArea();
                String address1 = address.getAddress();
                String c_address = area + address1;
                info = new ConsigneeInfo(name, phone, c_address);
                affirm_address.setInfo(info);
//                info.setName(name);
//                info.setPhone(phone);
//                info.setAddress(c_address);
//                Log.d(TAG, "********************" + info);
                adapter.notifyDataSetChanged();
            } catch (NullPointerException e) {
            }

        }
    }

    @Override
    public void click(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.affirm_listview_consignee_qzf:
                startActivity(new Intent(AffirmReturnActivity.this, WayofPayActivity.class));
                break;
            case R.id.affirm_listview_consignee:
                startActivityForResult(new Intent(AffirmReturnActivity.this, ChooseAddressActivity.class), 0);
                break;
        }
//        Toast.makeText(AffirmReturnActivity.this,"点击了去支付按钮",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void response(List<Address> list) {
        try {
            Address address = list.get(0);
            String name = address.getName();
            String phone = address.getPhone();
            String area = address.getArea();
            String address1 = address.getAddress();
            String c_address = area + address1;
            info = new ConsigneeInfo(name, phone, c_address);
            affirm_address.setInfo(info);
//                info.setName(name);
//                info.setPhone(phone);
//                info.setAddress(c_address);
//            Log.d(TAG, "********************" + info);
            adapter.notifyDataSetChanged();
        } catch (NullPointerException e) {

        } catch (IndexOutOfBoundsException e) {
            //数据库中没有地址，写点什么默认的
        }
    }
}
